package Capa_Fisica;

import Enlace.Util;
import Enlace.mySingleton;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

public class PuertoSerial {

    OutputStream out;
    String port;
    private Vector<Integer> mensaje;

    public Vector<Integer> getMensaje() {
        return mensaje;
    }

    public PuertoSerial() {
        super();
        mensaje = new Vector<Integer>();
        
    }
    
    public void borrarTrama() {
        mensaje.clear();
    }

    public void connect(String portName) throws Exception {
        port = portName;
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                InputStream in = serialPort.getInputStream();
                out = serialPort.getOutputStream();
                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }
    }

    /** */
    public class SerialReader implements Runnable {

        InputStream in;

        public SerialReader(InputStream in) {
            this.in = in;
        }

        public void run() {
            byte[] buffer = new byte[1000];
            int len = -1;
            Vector<Integer> temp = new Vector<Integer>();
            try {
                while ((len = this.in.read(buffer)) > -1) {
                    for (int i = 0; i < len; i++) {
                        temp.add((int) buffer[i]);
                        //se añade todo lo que pasa por el medio.. al vector
                        // el control se hace en la capa dos
                        if (temp.size() == 83) {
                            //Una vez el vector tiene la lngitud de una trama pues se crea el mensaje
                            mensaje = new Vector<Integer>(temp);
                            System.out.println("***"+mensaje);
                            mySingleton.getInstance(port).process(Util.getInstance().toString(mensaje));
                            temp.clear();
                        }
                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /** */
    public class SerialWriter implements Runnable {

        OutputStream out;

        public SerialWriter(OutputStream out) {
            this.out = out;
        }

        public void run() {
            try {
                int c = 0;
                while ((c = System.in.read()) > -1) {
                    //out.flush();
                    this.out.write(c);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void enviarTrama(Vector<Integer> v) {
        try {
            for (int i = 0; i < v.size(); i++) {
                int unEntero = v.get(i);
                out.flush();
                this.out.write(unEntero);
               
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.exit(0);
        }

    }

    public OutputStream getOut() {
        return out;
    }

    public void setOut(OutputStream out) {
        this.out = out;
    }
}
